/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdVerDocumentos.java
 *
 * Created on 09-nov-2010, 15:53:09
 */

package Ordenes;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.MiTablePrint;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.Utiles_PDF;
import java.awt.event.KeyEvent;
import java.io.ByteArrayInputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import suministros.ejb30.TO.ComprobantePagoTO;

/**
 *
 * @author Luis_Angel
 */
public class jdVerDocumentos extends javax.swing.JDialog {

    private String nro_orden;
    private BindingGroup bindingGroup;
    private JTableBinding jTableBinding;
    private List<ComprobantePagoTO> comprobantes;
    /** Creates new form jdVerDocumentos */
    public jdVerDocumentos(java.awt.Frame parent, boolean modal, String nro_orden) {
        
        super(parent, modal);
        this.nro_orden = nro_orden;
        initComponents();
        try {
            comprobantes = GestionPedidosDelegate.getInstance().getComprobantesOrden(nro_orden);
            actualizaTablaComprobantes(comprobantes);
        } catch (Exception ex) {
            Logger.getLogger(jdVerDocumentos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtComprobantes = new javax.swing.JTable();
        this.jtComprobantes.getTableHeader().setReorderingAllowed(false) ;
        jbDetalles = new javax.swing.JButton();
        jbtImprimir = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Comprobantes Encontrados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), java.awt.Color.blue)); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        jtComprobantes.setAutoCreateRowSorter(true);
        jtComprobantes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nro Orden", "Comprobante", "Serie", "Numero", "Fecha Registro", "Fecha Pago", "RUC", "Zona"
            }
        ));
        jScrollPane1.setViewportView(jtComprobantes);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jbDetalles.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jbDetalles.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/view_detailed.png"))); // NOI18N
        jbDetalles.setMnemonic('D');
        jbDetalles.setText("VER DOCUMENTO");
        jbDetalles.setToolTipText("Cancelar la Operacion");
        jbDetalles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDetallesActionPerformed(evt);
            }
        });

        jbtImprimir.setFont(new java.awt.Font("Arial", 1, 12));
        jbtImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/document_print.png"))); // NOI18N
        jbtImprimir.setText("IMPRIMIR");
        jbtImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtImprimirActionPerformed(evt);
            }
        });
        jbtImprimir.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtImprimirKeyPressed(evt);
            }
        });

        jbCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CERRAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 669, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 649, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jbDetalles)
                            .addGap(274, 274, 274)
                            .addComponent(jbtImprimir)
                            .addGap(18, 18, 18)
                            .addComponent(jbCancelar)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(6, 6, 6)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jbtImprimir)
                        .addComponent(jbCancelar)
                        .addComponent(jbDetalles))
                    .addContainerGap(12, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     private void actualizaTablaComprobantes(List<ComprobantePagoTO> comprobantes) {
            this.bindingGroup = new BindingGroup();
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ,comprobantes , this.jtComprobantes);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${nro_orden}"));

            columnBinding0.setColumnName("Nro Orden");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${tipo_comprobante}"));

            columnBinding1.setColumnName("Comprobante");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${serie_comprobante}"));

            columnBinding2.setColumnName("Serie");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${numero_comprobante}"));

            columnBinding3.setColumnName("Numero");
            columnBinding3.setColumnClass(String.class);
            columnBinding3.setEditable(false);

            JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha_ingreso}"));

            columnBinding4.setColumnName("Fecha Registro");
            columnBinding4.setColumnClass(String.class);
            columnBinding4.setEditable(false);

            JTableBinding.ColumnBinding columnBinding5 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha_pago}"));

            columnBinding5.setColumnName("Fecha Pago");
            columnBinding5.setColumnClass(String.class);
            columnBinding5.setEditable(false);

            JTableBinding.ColumnBinding columnBinding6 = this.jTableBinding.addColumnBinding(ELProperty.create("${ruc_proveedor}"));

            columnBinding6.setColumnName("RUC");
            columnBinding6.setColumnClass(String.class);
            columnBinding6.setEditable(false);

            JTableBinding.ColumnBinding columnBinding7 = this.jTableBinding.addColumnBinding(ELProperty.create("${zona}"));

            columnBinding7.setColumnName("Zona");
            columnBinding7.setColumnClass(String.class);
            columnBinding7.setEditable(false);

            JTableBinding.ColumnBinding columnBinding8 = this.jTableBinding.addColumnBinding(ELProperty.create("${monto}"));

            columnBinding8.setColumnName("Monto");
            columnBinding8.setColumnClass(BigDecimal.class);
            columnBinding8.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();

        }

    private void jbDetallesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDetallesActionPerformed

        int filaElegida = this.jtComprobantes.getSelectedRow();

        if (filaElegida < 0) {
            Utiles_MensajesDialogo.mensajeError("Debe elegir un Comprobante!");
            this.jtComprobantes.requestFocus();
            return;
        }

        try {
            ComprobantePagoTO cpto = (ComprobantePagoTO)this.comprobantes.get(this.jtComprobantes.convertRowIndexToModel(filaElegida));
            byte[] archivo = GestionPedidosDelegate.getInstance().getComprobanteArchivo(cpto.getId_comprobante());
            //Reportes.verComprobante(cpto.getId_comprobante());
            if(archivo!=null){
                ByteArrayInputStream is = new ByteArrayInputStream(archivo);
                Utiles_PDF.verArchivo(is);
            } else
                Utiles_MensajesDialogo.mensajeAlerta("El comprobante no tiene asignado un archivo");

            //  Reportes.verImagen(is);
            //EPSPDFViewer visorpdf = new EPSPDFViewer(archivo);
            //visorpdf.mostrar();



        } catch (Exception e) {
            e.printStackTrace();
            Utiles_MensajesDialogo.mensajeError("No se pudo Cargar el archivo de comprobante\n" + e.getMessage());
        }
}//GEN-LAST:event_jbDetallesActionPerformed

    private void jbtImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtImprimirActionPerformed

        MiTablePrint pp = new MiTablePrint(this.jtComprobantes, "COMPROBANTES", "DETALLE DE COMPROBANTES");
        pp.showPrintPreview();
}//GEN-LAST:event_jbtImprimirActionPerformed

    private void jbtImprimirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtImprimirKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbtImprimir.doClick();
        }
}//GEN-LAST:event_jbtImprimirKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed

}//GEN-LAST:event_jbCancelarKeyPressed

  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbDetalles;
    private javax.swing.JButton jbtImprimir;
    private javax.swing.JTable jtComprobantes;
    // End of variables declaration//GEN-END:variables

}
